; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -passes=gvn -S < %s | FileCheck %s

; Memdep had funny bug related to invariant.groups - because it did not
; invalidated cache, in some very rare cases it was possible to show memory
; dependence of the instruction that was deleted, but because other instruction
; took it's place it resulted in call to vtable! Removing any of the branch
; hides the bug.

target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-grtev4-linux-gnu"

%0 = type { ptr, %1 }
%1 = type { %2 }
%2 = type { %3 }
%3 = type { %4, i64, %5 }
%4 = type { ptr }
%5 = type { i64, [8 x i8] }

define void @fail(ptr noalias sret(i1) %arg, ptr %arg1, ptr %arg2, ptr %arg3) local_unnamed_addr #0 {
; CHECK-LABEL: @fail(
; CHECK-NEXT:  bb:
; CHECK-NEXT:    [[I4:%.*]] = load ptr, ptr [[ARG1:%.*]], align 8, !invariant.group !6
; CHECK-NEXT:    [[I5:%.*]] = getelementptr inbounds ptr, ptr [[I4]], i64 6
; CHECK-NEXT:    [[I6:%.*]] = load ptr, ptr [[I5]], align 8, !invariant.load !6
; CHECK-NEXT:    [[I7:%.*]] = tail call i64 [[I6]](ptr [[ARG1]]) #[[ATTR1:[0-9]+]]
; CHECK-NEXT:    [[I9:%.*]] = load ptr, ptr [[ARG2:%.*]], align 8
; CHECK-NEXT:    store i8 0, ptr [[I9]], align 1
; CHECK-NEXT:    br i1 undef, label [[BB10:%.*]], label [[BB29:%.*]]
; CHECK:       bb10:
; CHECK-NEXT:    [[I14_PHI_TRANS_INSERT:%.*]] = getelementptr inbounds ptr, ptr [[I4]], i64 22
; CHECK-NEXT:    [[I15_PRE:%.*]] = load ptr, ptr [[I14_PHI_TRANS_INSERT]], align 8, !invariant.load !6
; CHECK-NEXT:    br label [[BB12:%.*]]
; CHECK:       bb12:
; CHECK-NEXT:    [[I16:%.*]] = call i64 [[I15_PRE]](ptr nonnull [[ARG1]], ptr null, i64 0) #[[ATTR1]]
; CHECK-NEXT:    br i1 undef, label [[BB28:%.*]], label [[BB17:%.*]]
; CHECK:       bb17:
; CHECK-NEXT:    br i1 undef, label [[BB18:%.*]], label [[BB21:%.*]]
; CHECK:       bb18:
; CHECK-NEXT:    br label [[BB28]]
; CHECK:       bb21:
; CHECK-NEXT:    br i1 undef, label [[BB25:%.*]], label [[BB26:%.*]]
; CHECK:       bb25:
; CHECK-NEXT:    br label [[BB28]]
; CHECK:       bb26:
; CHECK-NEXT:    br label [[BB28]]
; CHECK:       bb28:
; CHECK-NEXT:    br i1 undef, label [[BB12]], label [[BB29]]
; CHECK:       bb29:
; CHECK-NEXT:    ret void
;
bb:
  %i4 = load ptr, ptr %arg1, align 8, !invariant.group !6
  %i5 = getelementptr inbounds ptr, ptr %i4, i64 6
  %i6 = load ptr, ptr %i5, align 8, !invariant.load !6
  %i7 = tail call i64 %i6(ptr %arg1) #1
  %i9 = load ptr, ptr %arg2, align 8
  store i8 0, ptr %i9, align 1
  br i1 undef, label %bb10, label %bb29

bb10:                                             ; preds = %bb
  br label %bb12

bb12:                                             ; preds = %bb28, %bb10
  %i13 = load ptr, ptr %arg1, align 8, !invariant.group !6
  %i14 = getelementptr inbounds ptr, ptr %i13, i64 22
  %i15 = load ptr, ptr %i14, align 8, !invariant.load !6
  %i16 = call i64 %i15(ptr nonnull %arg1, ptr null, i64 0) #1
  br i1 undef, label %bb28, label %bb17

bb17:                                             ; preds = %bb12
  br i1 undef, label %bb18, label %bb21

bb18:                                             ; preds = %bb17
  br label %bb19

bb19:                                             ; preds = %bb18
  br label %bb20

bb20:                                             ; preds = %bb19
  br label %bb28

bb21:                                             ; preds = %bb17
  br label %bb22

bb22:                                             ; preds = %bb21
  br label %bb23

bb23:                                             ; preds = %bb22
  br label %bb24

bb24:                                             ; preds = %bb23
  br i1 undef, label %bb25, label %bb26

bb25:                                             ; preds = %bb24
  br label %bb28

bb26:                                             ; preds = %bb24
  br label %bb27

bb27:                                             ; preds = %bb26
  br label %bb28

bb28:                                             ; preds = %bb27, %bb25, %bb20, %bb12
  br i1 undef, label %bb12, label %bb29

bb29:                                             ; preds = %bb28, %bb
  ret void
}

attributes #0 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="non-leaf" "less-precise-fpmad"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+popcnt,+sse,+sse2,+sse3,+sse4.1,+sse4.2,+ssse3,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { nounwind uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="non-leaf" "less-precise-fpmad"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+popcnt,+sse,+sse2,+sse3,+sse4.1,+sse4.2,+ssse3,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }

!llvm.linker.options = !{}
!llvm.module.flags = !{!0, !1, !3, !4, !5}

!0 = !{i32 1, !"StrictVTablePointers", i32 1}
!1 = !{i32 3, !"StrictVTablePointersRequirement", !2}
!2 = !{!"StrictVTablePointers", i32 1}
!3 = !{i32 1, !"wchar_size", i32 4}
!4 = !{i32 8, !"PIC Level", i32 2}
!5 = !{i32 7, !"PIE Level", i32 2}
!6 = !{}
